package com.ethannhu.todaynews.component.content

import android.content.Context
import android.view.ViewGroup
import android.widget.ImageView
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.ethannhu.todaynews.R
import com.ethannhu.todaynews.util.ImageUtil
import com.luck.picture.lib.entity.LocalMedia


/**
 * 列表内多张图片适配器
 */
class ImageAdapter : BaseQuickAdapter<Any, QuickViewHolder>() {
    override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: Any?) {
        item?.let {
            val iconView = holder.getView<ImageView>(R.id.icon)
            when (item) {
                is String -> {
                    ImageUtil.show(iconView, item)
                }

                is LocalMedia -> {
                    //选择的图片
                    ImageUtil.showLocalImage(iconView, item.availablePath)

                    //显示删除按钮
                    holder.setGone(R.id.close, false)
                }

                else -> {
                    iconView.setImageResource(item as Int)
                }
            }
        }
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
        return QuickViewHolder(R.layout.item_image, parent)
    }

}